<?php
class UserController extends AdminController{
    public function indexAction(){
        $model = new UserModel;
        $data = $model->select();
        $this->_view->assign('data',$data);
        // var_dump($data);
    }
    public function listAction(){
        $model = new UserModel;
        $user = $model->select();
        $this->_view->user = $user;
    }
    public function addAction(){
        $model = new UserRoleModel;
        $role = $model->select();
        $this->_view->role = $role;
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $model = new UserModel;
        $data['user_name'] = Request::aspost(6,'user_name');
        $data['role_id'] = Request::aspost(3,'role_id');
        $user_password = Request::aspost(6,'user_password');
        $salt = Request::aspost(6,'salt','');
        if(empty($user_password)){
            Response::error('Lost the Password');
            return false;
        }else{
            // Mark
            $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
            $data['salt'] = base64_encode($salt);
            $options = [
                'cost' => 11,
                'salt' => $salt,
            ];
            $data['user_password'] = password_hash($user_password,PASSWORD_BCRYPT,$options);
        }
        $flag = $model->insert($data);
        if($flag == 0 || $flag == false){
            Response::error('The user name was used');
        }else{
            Response::success('nice');
        }
    }

    public function editAction(){
        $id = Request::asget(3,'id',null);
        if(null === $id){
            die;
        }else{
            $model = new UserModel;
            $model = new UserRoleModel;
            $role = $model->select();
            $this->_view->role = $role;
            $model = new UserModel;
            $user = $model->where(['user_id'=>$id])->one();
            $this->_view->user = $user;

        }
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if(empty($id)){
            die;
        }else{
            $data['user_name'] = Request::aspost(6,'user_name',null);
            if($data['user_name'] === null){
                Response::error('Forget user name');
                return false;
            }else{
                $password  = Request::aspost(6,'user_password','');
                if(!empty($password)){
                    // Mark...
                    $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
                    $options = [
                        'cost' => 11,
                        'salt' => $salt,
                    ];
                    $data['user_password'] = password_hash($password,PASSWORD_BCRYPT,$options);
                }
                $model = new UserModel;
                $flag = $model->where(['user_id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('The user name was used');
                }else{
                    Response::success('nice');
                }
                
            }
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            // $status = Request::aspost(3,'status','');
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if($status == ''){
                return false;
            }else{
                
                switch($status){
                case '0': $data['status'] = 1;
                    break;
                case '1': $data['status'] = 0;
                    break;
                }
                $model = new UserModel;
                $flag = $model->where(['user_id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('');
                }else{
                    Response::success('nice');
                }
            }
        }
    }
    

                
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if(null == $id){
            die;
        }else{
            $log_model = new UserModel;
            $flag = $log_model->where(['user_id' => $id])->delete();
            if($flag == 0){
                return Response::success('nice');
                
            }else{
                return Response::error('error');
            }            
        }

    }
    public function removeLogAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::filter(1,'id',3);
        if(null == $id){
            die;
        }else{
            $log_model = new UserLogModel;
            $log_model->where(['log_id' => $id])->delete();
            Response::success('删除成功');
        }

    }
    public function log2Action(){
        $page = Request::filter(0,'page',3);
        $user = Yaf_Session::getInstance()->get('user');
        $id = isset($_GET['id']) ? Request::filter(0,'id',3) : $user['id'];
        $model = new UserLogModel;
        $data = $model->page($page,$id);            
        if(null == $page){
            $this->_view->count = $data['count'];
            $this->_view->data = $data['log'];
            $this->_view->uid = $id;
        }else{
            Yaf_Dispatcher::getInstance()->disableView();
            // var_dump($data);
            echo $this->render('log.tpl',$data);            
        }
    }
    public function cryptAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $str = '1234567';
        $v = md5($str);
        $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
        $salt = '/s�Q./؍}lI�����8�b';
        echo $salt;
        $options = [
            'cost' => 11,
            // 'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
            // 'salt' => '1234567'
            'salt' => $salt,
        ];
        $v = password_hash($str,PASSWORD_BCRYPT,$options);
        var_dump($str);
        echo $v;
        var_dump('$2y$11$L3O5US4v2I19bEnklgjE3OFl5i4DvW1U.CO3c3D7sv656HdgD1feu');

    }
    public function hashAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $str = '123456789';
        $model = new UserModel;
        $data['salt'] = '';
        $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
        // $salt 
        // $data['salt'] = 
        $options = [
            'cost' => 11,
            'salt' => $salt,
        ];
        // var_dump($salt);
        
        $data['salt'] = base64_encode($salt);
        $data['user_password'] = password_hash($str,PASSWORD_BCRYPT,$options);
        var_dump($data);
        $model->where(['user_id' => 6])->update($data);
    }
    public function verfityAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $password = '123456789';
        $model = new UserModel;
        $data = $model->where(['user_id'=>6])->one();
        // $salt = base64_decode($data['salt']);
        $flag = password_verify($password,$data['user_password']);
        // if ($pass_crypt == crypt($password, $)) {
        //     echo "Success! Valid password";
        // } else {
        //     echo "Invalid password";
        // }
        var_dump($flag);
    }
        
}
?>
